package org.ezjava.rpc.consumer;

import org.apache.thrift.TConfiguration;
import org.apache.thrift.TException;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;
import org.apache.thrift.transport.TTransportException;

/**
 * @author Erik
 * @create 2021-08-04 17:24
 * @description
 **/
public class HelloServiceClient {

    public void startClient(String username){
        TTransport tTransport = null;
        try {
            TConfiguration conf = new TConfiguration();
            tTransport = new TSocket("localhost",8888);
            // 协议要和服务端一致
            TProtocol protocol = new TBinaryProtocol(tTransport);
//            TProtocol protocol = new TCompactProtocol(tTransport);
//            TProtocol protocol = new TJSONProtocol(tTransport);
            HelloWorldService.Client client = new HelloWorldService.Client(protocol);
            tTransport.open();
//            String result = client.helloString(username);
            String result = client.sayHello(username);
            log.info("Thrify client result = " + result);
        } catch (TTransportException ex) {
            ex.printStackTrace();
        } catch (TException ex) {
            ex.printStackTrace();
        }finally {
            if(tTransport != null){
                tTransport.close();
            }
        }
    }
    public static void main(String[] args){
        HelloServiceClient client = new HelloServiceClient();
        client.startClient(" YP");
    }
}
